﻿using System;
using System.Linq;
using Fonour.Domain.Entities;
using Fonour.EntityFrameworkCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace Fonour.EntityFrameworkCore
{
    public static class SeedData
    {
        public static void Initialize(IApplicationBuilder applicationBuilder)
        {

            using (var serviceScope = applicationBuilder.ApplicationServices.CreateScope())

            {
                var context = serviceScope.ServiceProvider.GetRequiredService<FonourDbContext>();

                //if (context.Users.Any())
                //{
                //    return;
                //}

                Guid deparmentId = Guid.NewGuid();

                context.Deparments.Add(new Deparment()
                {
                    Id = deparmentId,
                    Name = "Fonout集团总部",
                    ParentId = Guid.NewGuid()
                });

                context.Users.Add(new User()
                {
                    UserName = "admin",
                    Password = "123456",
                    Name = "超级管理员",
                    DepartmentId = deparmentId
                });

                context.Menus.AddRange(
                    new Menu()
                    {
                        Name = "组织机构管理",
                        Code = "Deparment",
                        Serialnumber = 0,
                        ParentId = Guid.NewGuid(),
                        Icon = "fa fa-link"
                    }, new Menu()
                    {
                        Name = "角色管理",
                        Code = "Role",
                        Serialnumber = 1,
                        ParentId = Guid.NewGuid(),
                        Icon = "fa fa-link"
                    }, new Menu()
                    {
                        Name = "用户管理",
                        Code = "User",
                        Serialnumber = 2,
                        ParentId = Guid.NewGuid(),
                        Icon = "fa fa-link"
                    }, new Menu()
                    {
                        Name = "功能管理",
                        Code = "Deparment",
                        Serialnumber = 3,
                        ParentId = Guid.NewGuid(),
                        Icon = "fa fa-link"
                    }
                );
                context.SaveChanges();
            }
        }
    }

}

